﻿package org.xhs.report.common.ui.panel
{
    import baidu.lib.utils.*;
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.ui.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.net.*;
    import flash.text.*;

    public class StorePage extends BlankPage
    {
        protected var _storePanel:StorePanel;
        protected var _storeBuyBtn:Button;
        protected var _storeTxt:TextField;
        protected var _storeSlideCtner:Sprite;
        protected var _storeSliderMask:Sprite;
        protected var _storeimageHeight:int = 117;
        protected var _storeimageWidth:int = 92;
        protected var _storeItemGap:int = 16;
        protected var _storeItemMargin:Number;
        protected var _storeItemShow:int;
        protected var _storeItemArray:Array;
        protected var _storeItemLoaderArray:Array;
        protected var _storeItemTextArray:Array;
        protected var _currentStoreItem:int = 0;
        protected var _storeLeftArrow:Button;
        protected var _storeRightArrow:Button;
        protected var _storeEndText:TextField;
        protected var _storeArrowPace:int = 3;
        protected var storeModel:StoreModel;
        protected var screenModel:ScreenModel;

        public function StorePage()
        {
            return;
        }// end function

        override protected function initPageLoading() : void
        {
            return;
        }// end function

        override public function showLoading() : void
        {
            return;
        }// end function

        override public function hideLoading() : void
        {
            return;
        }// end function

        public function init() : void
        {
            this.hideLoading();
            mouseChildren = true;
            this.storeModel = InstanceManager.createSingletonInstance(StoreModel);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.resetStorePage();
            return;
        }// end function

        public function resetStorePage() : void
        {
            if (!this.storeModel.isStore)
            {
                return;
            }
            this.setSize(Config.pageWidth, Config.pageHeight, false);
            if (this.storeModel.isFree)
            {
                if (this.storeModel.hasRecom)
                {
                    this.resetRecomPanel();
                }
            }
            else
            {
                this.resetBuyPanel();
            }
            return;
        }// end function

        protected function resetBuyPanel() : void
        {
            if (!this._storePanel)
            {
                this._storePanel = new StorePanel();
                this._storeBuyBtn = new Button();
                this._storeBuyBtn.buttonMode = true;
                this._storeBuyBtn.useHandCursor = true;
                this._storeBuyBtn.setStyle("skin", StoreBuyBtn);
                this._storeBuyBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.storeBuyDown);
                this._storeBuyBtn.addEventListener(MouseEvent.MOUSE_MOVE, this.showHandCursor);
                this._storeBuyBtn.addEventListener(MouseEvent.MOUSE_OUT, this.hideHandCursor);
                this._storeBuyBtn.addEventListener(MouseEvent.MOUSE_UP, this.showHandCursor);
                this._storeBuyBtn.x = 57;
                this._storeBuyBtn.y = 84;
                this._storePanel.addChild(this._storeBuyBtn);
            }
            if (!this._storeTxt)
            {
                this._storeTxt = new TextField();
                this._storeTxt.defaultTextFormat = new TextFormat("宋体", 14, 0);
                this._storeTxt.autoSize = "left";
                this._storeTxt.selectable = false;
                this._storeTxt.x = 56;
                this._storeTxt.y = 52;
                this._storeTxt.htmlText = "身价是:<b><FONT COLOR=\"#f37605\">￥<FONT FACE=\"Arial\">" + NumberFormatter.format(this.storeModel.storePrice, "II.DD") + "</FONT></FONT></b>，比纸质书便宜<b><FONT FACE=\"Arial\" COLOR=\"#f37605\">" + this.storeModel.storeDiscount + "%</FONT></b>";
                this._storePanel.addChild(this._storeTxt);
            }
            var _loc_1:int = 0;
            this._storePanel.y = 0;
            this._storePanel.x = _loc_1;
            this._storePanel.x = (width - 340) / 2;
            this._storePanel.y = Math.min(240, (height - this._storePanel.height) / 2);
            addChild(this._storePanel);
            return;
        }// end function

        protected function resetRecomPanel() : void
        {
            var bitmapData:BitmapData;
            var bitmap:Bitmap;
            var i:int;
            var item:Sprite;
            var loader:Loader;
            var textField:TextField;
            var textString:String;
            var json:* = this.storeModel.recomData;
            if (json && json.length)
            {
                this._storeItemMargin = 50;
                if (!this._storeSlideCtner)
                {
                    this._storeSlideCtner = new Sprite();
                    addChild(this._storeSlideCtner);
                    this._storeLeftArrow = new Button();
                    addChild(this._storeLeftArrow);
                    this._storeLeftArrow.setStyle("skin", StoreArrowLeft);
                    this._storeLeftArrow.drawNow();
                    this._storeLeftArrow.buttonMode = true;
                    this._storeLeftArrow.useHandCursor = true;
                    this._storeLeftArrow.addEventListener(MouseEvent.MOUSE_DOWN, function (event:MouseEvent) : void
            {
                event.stopPropagation();
                _currentStoreItem = _currentStoreItem - _storeArrowPace;
                arrangeStoreItems();
                return;
            }// end function
            );
                    this._storeLeftArrow.addEventListener(MouseEvent.MOUSE_MOVE, this.showHandCursor);
                    this._storeLeftArrow.addEventListener(MouseEvent.MOUSE_OUT, this.hideHandCursor);
                    this._storeLeftArrow.addEventListener(MouseEvent.MOUSE_UP, this.showHandCursor);
                    this._storeRightArrow = new Button();
                    addChild(this._storeRightArrow);
                    this._storeRightArrow.setStyle("skin", StoreArrowRight);
                    this._storeRightArrow.drawNow();
                    this._storeRightArrow.buttonMode = true;
                    this._storeRightArrow.useHandCursor = true;
                    this._storeRightArrow.addEventListener(MouseEvent.MOUSE_DOWN, function (event:MouseEvent) : void
            {
                event.stopPropagation();
                _currentStoreItem = _currentStoreItem + _storeArrowPace;
                arrangeStoreItems();
                return;
            }// end function
            );
                    this._storeRightArrow.addEventListener(MouseEvent.MOUSE_MOVE, this.showHandCursor);
                    this._storeRightArrow.addEventListener(MouseEvent.MOUSE_OUT, this.hideHandCursor);
                    this._storeRightArrow.addEventListener(MouseEvent.MOUSE_UP, this.showHandCursor);
                    this._storeSliderMask = new Sprite();
                    addChild(this._storeSliderMask);
                    this._storeSlideCtner.mask = this._storeSliderMask;
                    this._storeEndText = new TextField();
                    this._storeEndText.selectable = false;
                    this._storeEndText.mouseEnabled = false;
                    this._storeEndText.autoSize = "left";
                    this._storeEndText.defaultTextFormat = new TextFormat(null, 16, 0, true);
                    this._storeEndText.text = "感谢您的阅读\n\n\n\n我们猜您还喜欢：";
                    bitmapData = new BitmapData(this._storeEndText.width, this._storeEndText.height, true);
                    bitmapData.draw(this._storeEndText);
                    bitmap = new Bitmap(bitmapData, "auto", true);
                    bitmap.x = 16;
                    bitmap.y = 80;
                    addChild(bitmap);
                }
                this._storeItemShow = Math.floor((_pageWidth - this._storeItemMargin * 2 + this._storeItemGap) / (this._storeimageWidth + this._storeItemGap));
                this._storeArrowPace = this._storeItemShow;
                this._storeItemMargin = (_pageWidth + this._storeItemGap - this._storeItemShow * (this._storeimageWidth + this._storeItemGap)) / 2;
                var _loc_2:* = this._storeItemMargin;
                this._storeSliderMask.x = this._storeItemMargin;
                this._storeSlideCtner.x = _loc_2;
                var _loc_2:* = 480 * 2 / 5;
                this._storeSliderMask.y = 480 * 2 / 5;
                this._storeSlideCtner.y = _loc_2;
                var _loc_2:* = this._storeSlideCtner.y + 160 / 2;
                this._storeRightArrow.y = this._storeSlideCtner.y + 160 / 2;
                this._storeLeftArrow.y = _loc_2;
                this._storeSliderMask.graphics.clear();
                this._storeSliderMask.graphics.beginFill(0);
                this._storeSliderMask.graphics.drawRect(-2, -2, _pageWidth - this._storeItemMargin * 2 + 4, this._storeimageHeight + 120);
                this._storeSliderMask.graphics.endFill();
                if (!this._storeItemArray)
                {
                    this._storeItemArray = [];
                    this._storeItemTextArray = [];
                    this._storeItemLoaderArray = [];
                    i;
                    while (i < json.length)
                    {
                        
                        item = new Sprite();
                        item.name = "item_" + i;
                        TooltipManager.getInstance().register(item, json[i].tit);
                        this._storeItemArray.push(item);
                        item.mouseChildren = false;
                        item.buttonMode = true;
                        item.useHandCursor = true;
                        item.addEventListener(MouseEvent.MOUSE_DOWN, this.itemMouseDown);
                        item.addEventListener(MouseEvent.MOUSE_MOVE, this.itemMouseMove);
                        item.addEventListener(MouseEvent.MOUSE_OUT, this.itemMouseOut);
                        item.addEventListener(MouseEvent.MOUSE_UP, this.itemMouseMove);
                        item.x = (this._storeItemGap + this._storeimageWidth) * i;
                        this._storeSlideCtner.addChild(item);
                        loader = new Loader();
                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (event:Event) : void
            {
                var _loc_2:* = event.target.loader as Loader;
                var _loc_3:* = Math.min(_storeimageWidth / _loc_2.width, _storeimageHeight / _loc_2.height);
                _loc_2.scaleY = Math.min(_storeimageWidth / _loc_2.width, _storeimageHeight / _loc_2.height);
                _loc_2.scaleX = _loc_3;
                _loc_2.x = (_storeimageWidth - _loc_2.width) / 2;
                _loc_2.y = (_storeimageHeight - _loc_2.height) / 2;
                return;
            }// end function
            );
                        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (event:IOErrorEvent) : void
            {
                return;
            }// end function
            );
                        loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (event:SecurityErrorEvent) : void
            {
                return;
            }// end function
            );
                        loader.load(new URLRequest(json[i].img));
                        this._storeItemLoaderArray.push(loader);
                        textField = new TextField();
                        textField.wordWrap = true;
                        textField.mouseEnabled = false;
                        textField.selectable = false;
                        textField.width = this._storeimageWidth;
                        textField.defaultTextFormat = new TextFormat(null, null, null, null, null, null, null, null, "left");
                        textField.htmlText = "<FONT COLOR=\"#014A8F\">" + json[i].tit + "</FONT>";
                        textString = textField.text;
                        while (textField.numLines > 2)
                        {
                            
                            textString = textString.substr(0, (textString.length - 1));
                            textField.htmlText = "<FONT COLOR=\"#014A8F\">" + textString + "..." + "</FONT>";
                        }
                        textField.htmlText = textField.htmlText + ("\n<FONT COLOR=\"#000000\">作者: " + json[i].aut + "</FONT>");
                        textField.y = this._storeimageHeight + 4;
                        this._storeItemTextArray.push(textField);
                        i = (i + 1);
                    }
                }
                this.arrangeStoreItems();
            }
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number, param3:Boolean = false) : void
        {
            if (param1 <= 0 || param2 <= 0)
            {
                return;
            }
            _pageWidth = param1;
            _pageHeight = param2;
            var _loc_4:* = pageContainer.graphics;
            pageContainer.graphics.clear();
            _loc_4.beginFill(16645629, 1);
            _loc_4.drawRect(0, 0, _pageWidth, _pageHeight);
            _loc_4.endFill();
            if (param3)
            {
                this.initPageLoading();
            }
            return;
        }// end function

        protected function itemMouseDown(event:MouseEvent) : void
        {
            event.stopPropagation();
            BrowserUtil.openNewWindow(this.storeModel.recomData[int(String(event.currentTarget.name).split("_")[1])].link);
            WenkuLog.addLog(WenkuLog.LOGURL_STORE_SLIDER);
            return;
        }// end function

        protected function itemMouseMove(event:MouseEvent) : void
        {
            this.itemFrameReset(event.currentTarget as Sprite, true);
            event.stopPropagation();
            this.showHandCursor();
            return;
        }// end function

        protected function itemMouseOut(event:MouseEvent) : void
        {
            this.itemFrameReset(event.currentTarget as Sprite, false);
            event.stopPropagation();
            this.hideHandCursor();
            return;
        }// end function

        protected function itemFrameReset(param1:Sprite, param2:Boolean = false) : void
        {
            if (param1)
            {
                param1.graphics.clear();
                param1.graphics.lineStyle(1, param2 ? (15159809) : (15066340));
                param1.graphics.drawRect(-1, -1, this._storeimageWidth + 2, this._storeimageHeight + 2);
                param1.graphics.endFill();
            }
            return;
        }// end function

        protected function itemFrameClear(param1:Sprite) : void
        {
            if (param1)
            {
                param1.graphics.clear();
            }
            return;
        }// end function

        protected function showHandCursor(event:MouseEvent = null) : void
        {
            CursorManager.getInstance().hideCurrentCursor();
            if (event)
            {
                event.stopPropagation();
            }
            return;
        }// end function

        protected function hideHandCursor(event:MouseEvent = null) : void
        {
            CursorManager.getInstance().showCursor(ReadCursor);
            if (event)
            {
                event.stopPropagation();
            }
            return;
        }// end function

        protected function storeBuyDown(event:MouseEvent) : void
        {
            event.stopPropagation();
            if (ExternalInterface.available && this.storeModel.storeCallBack)
            {
                try
                {
                    ExternalInterface.call(this.storeModel.storeCallBack);
                }
                catch (e:Error)
                {
                }
            }
            return;
        }// end function

        protected function arrangeStoreItems() : void
        {
            if (!this._storeItemArray || !this._storeItemArray.length)
            {
                return;
            }
            this._storeLeftArrow.enabled = true;
            this._storeRightArrow.enabled = true;
            this._storeLeftArrow.x = this._storeItemMargin - this._storeLeftArrow.width - 26;
            this._storeRightArrow.x = _pageWidth - this._storeItemMargin + this._storeLeftArrow.width + 26;
            if (this._currentStoreItem <= 0)
            {
                this._currentStoreItem = 0;
                this._storeLeftArrow.enabled = false;
            }
            if (this._currentStoreItem >= this._storeItemArray.length - this._storeItemShow - 1)
            {
                this._currentStoreItem = Math.max(0, (this._storeItemArray.length - 1) - this._storeItemShow);
                this._storeRightArrow.enabled = false;
            }
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < this._storeItemArray.length)
            {
                
                if (this._storeItemArray[_loc_1])
                {
                    this.itemFrameClear(this._storeItemArray[_loc_1] as Sprite);
                }
                if (this._storeItemArray[_loc_1] && this._storeItemTextArray[_loc_1] && this._storeItemArray[_loc_1].contains(this._storeItemTextArray[_loc_1]))
                {
                    this._storeItemArray[_loc_1].removeChild(this._storeItemTextArray[_loc_1]);
                }
                if (this._storeItemArray[_loc_1] && this._storeItemLoaderArray[_loc_1] && this._storeItemArray[_loc_1].contains(this._storeItemLoaderArray[_loc_1]))
                {
                    this._storeItemArray[_loc_1].removeChild(this._storeItemLoaderArray[_loc_1]);
                }
                _loc_1++;
            }
            _loc_1 = this._currentStoreItem;
            while (_loc_1 < Math.min(this._currentStoreItem + this._storeItemShow, this._storeItemArray.length))
            {
                
                this._storeItemArray[_loc_1].addChild(this._storeItemTextArray[_loc_1]);
                this._storeItemArray[_loc_1].addChildAt(this._storeItemLoaderArray[_loc_1], 0);
                this.itemFrameReset(this._storeItemArray[_loc_1]);
                _loc_1++;
            }
            TweenLite.killTweensOf(this._storeSlideCtner);
            TweenLite.to(this._storeSlideCtner, 0.6, {x:this._storeItemMargin - this._currentStoreItem * (this._storeimageWidth + this._storeItemGap), onUpdate:this.updateSliderItems});
            return;
        }// end function

        protected function updateSliderItems() : void
        {
            var _loc_1:Sprite = null;
            var _loc_2:int = 0;
            var _loc_3:* = this._storeItemArray.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_1 = this._storeItemArray[_loc_2] as Sprite;
                if (!_loc_1 || !this._storeItemTextArray[_loc_2] || !this._storeItemLoaderArray[_loc_2])
                {
                }
                else if (_loc_1.x + this._storeSlideCtner.x > this._storeSliderMask.x + this._storeSliderMask.width - 20 || _loc_1.x + this._storeimageWidth + this._storeSlideCtner.x < this._storeSliderMask.x)
                {
                    if (_loc_1.contains(this._storeItemTextArray[_loc_2]))
                    {
                        _loc_1.removeChild(this._storeItemTextArray[_loc_2]);
                    }
                    if (_loc_1.contains(this._storeItemLoaderArray[_loc_2]))
                    {
                        _loc_1.removeChild(this._storeItemLoaderArray[_loc_2]);
                    }
                }
                else
                {
                    _loc_1.addChild(this._storeItemTextArray[_loc_2]);
                    _loc_1.addChild(this._storeItemLoaderArray[_loc_2]);
                }
                _loc_2++;
            }
            return;
        }// end function

    }
}
